package com.ycc.service.Impl;

import com.ycc.dao.OnionLogRepository;
import com.ycc.model.dto.OnionLogDto;
import com.ycc.model.entity.OnionLog;
import com.ycc.service.OnionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OnionLogServiceImpl implements OnionLogService {

    @Autowired
    private OnionLogRepository onionLogRepository;

    @Override
    public List<OnionLog> findByUserId(Integer userId) {

//        return onionLogRepository.findByUserId(userId);
        // 根据时间倒叙查询
        return onionLogRepository.findByUserIdOrderByCreateTimeDesc(userId);
    }

    @Override
    public void save(OnionLogDto onionLogDto, Integer userId) {
        OnionLog onionLog = new OnionLog();
        onionLog.setContent(onionLogDto.getContent());
        onionLog.setLocation(onionLogDto.getLocation());
        onionLog.setMind(onionLogDto.getMind());
        onionLog.setUserId(userId);
        onionLog.setCreateTime(LocalDateTime.now());
        onionLogRepository.save(onionLog);
    }
}
